#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：model_study 
@File    ：run_mode.py
@Author  ：qsy
@Date    ：2024/10/15 20:16 
'''
from tensorflow.keras.models import load_model
from sklearn.preprocessing import StandardScaler
import joblib
from matplotlib import pyplot as plt
import numpy as np

# 导入matlab的预估函数
from matlab_flow import predict_flow
from matlab_head import predict_head

# 加载标准化器
scaler = joblib.load('scaler_X.pkl')
scaler2 = joblib.load('scaler_y.pkl')

# 加载模型 keras
model = load_model('my_best_model.keras')

# 打印model的结构
model.summary()

# 恒转速数据 转速2220 插值生成中间的数据

# 预测数据
# (1110, 142.60)
# (1110, 155.38)
# (1110, 169.28)
# (1110, 183.96)
# (1110, 197.26)
# (1110, 206.13)
# (1110, 211.33)
# (1110, 215.84)
# (1110, 217.96)

X_test = [[1110,142.60],
          [1110,155.38],
          [1110,169.28],
          [1110,183.96],
          [1110,197.26],
          [1110,206.13],
          [1110,211.33],
          [1110,215.84],
          [1110,217.96]]


# (1.21, 3.52)
# (3.23, 3.51)
# (5.06, 3.47)
# (7.06, 3.37)
# (9.15, 3.18)
# (11.14, 2.94)
# (13.01, 2.63)
# (14.52, 2.36)
# (15.55, 2.16)
Y_real = [[1.21, 3.52],
          [3.23, 3.51],
          [5.06, 3.47],
          [7.06, 3.37],
          [9.15, 3.18],
          [11.14, 2.94],
          [13.01, 2.63],
          [14.52, 2.36],
          [15.55, 2.16]]


Y_real = np.array(Y_real)

# ------------------------ 调用matlab函数进行预测 ----------
# 遍历测试集数据，调用matlab函数进行预测
y_pred_matlab = np.zeros((len(X_test), 2))
for i in range(len(X_test)):
    speed = X_test[i][0]
    power = X_test[i][1]
    flow = predict_flow(speed, power)
    head = predict_head(speed, power)
    y_pred_matlab[i][0] = flow
    y_pred_matlab[i][1] = head

# 绘制matlab预测结果与真实结果的散点图
plt.title('Matlab Fitting Predicted vs Real')
plt.scatter(Y_real[:, 0], Y_real[:, 1], label='real')
plt.scatter(y_pred_matlab[:, 0], y_pred_matlab[:, 1], label='pred')
plt.legend()
plt.show()

# 打印预测结果
# print('Matlab预测值：', y_pred_matlab)

# 计算误差
mse = np.mean((y_pred_matlab - Y_real) ** 2)
print('matlab MSE:', mse)


# ---------- 调用模型进行预测 ----------
# 标准化数据
X_test = scaler.transform(X_test)

# 预测结果
Y_pred = model.predict(X_test)

# 反标准化数据
Y_pred = scaler2.inverse_transform(Y_pred)

# 打印结果
# print('真实值：', Y_real)
# print('预测值：', Y_pred)


# 绘制预测结果与真实结果的散点图
plt.title('model Predicted vs Real')
plt.scatter(Y_real[:, 0], Y_real[:, 1], label='real')
plt.scatter(Y_pred[:, 0], Y_pred[:, 1], label='pred')
plt.legend()
plt.show()

# 计算误差
mse = np.mean((Y_pred - Y_real) ** 2)
print(' model MSE:', mse)

